package com.mylib.libcore.dialog;

import android.content.Context;
import android.graphics.Rect;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.PopupWindow;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.mylib.libcore.R;
import com.mylib.libcore.adapter.CommonDialogRecycleAdapter;
import com.mylib.libcore.network.rx.RxResultHelper;
import com.mylib.libcore.utils.JsonUtil;
import com.mylib.libcore.utils.ScreenUtil;
import com.mylib.libcore.utils.ToastUtil;
import com.mylib.libcore.utils.UIUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import io.reactivex.disposables.Disposable;


public class BottomRecyclePopupWindow extends PopupWindow {

    private View mMenuView;
    private RecyclerView recyclerView;
    private CommonDialogRecycleAdapter commonDialogRecycleAdapter;
    private Context mContent;
    private ItemClink mItemClink;
    private String mType;
    private List<String> listText = new ArrayList<>();
    public interface ItemClink{
        void itemTextClink(int index, String text, String id, String type);
    }
    public BottomRecyclePopupWindow(Context context, ItemClink itemClink, String type, List<String> listText) {
        super(context);
        this.listText = listText;
        this.mItemClink = itemClink;
        this.mContent = context;
        this.mType = type;
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mMenuView = inflater.inflate(R.layout.common_dialog_recycle_view, null);
        recyclerView = (RecyclerView) mMenuView.findViewById(com.mylib.libcore.R.id.recycler);
        try {
            UIUtils.setmargin(recyclerView,0, UIUtils.dp2px(300), 0, ScreenUtil.getNavigationBarHeight(context));
        }catch (Exception e){

        }
        initRecycle();

        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            this.setClippingEnabled(false);
        }

        //设置SelectPicPopupWindow的View
        this.setContentView(mMenuView);
        //设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(LayoutParams.FILL_PARENT);
        //设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(LayoutParams.MATCH_PARENT);
        //设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        //设置SelectPicPopupWindow弹出窗体动画效果
        this.setAnimationStyle(com.mylib.libcore.R.style.popwin_anim_style);
        //实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0xb0000000);
        //设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
        //mMenuView添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框
        mMenuView.findViewById(com.mylib.libcore.R.id.view_top).setOnClickListener(v ->  dismiss());
    }

    private void initRecycle(){
        commonDialogRecycleAdapter = new CommonDialogRecycleAdapter(R.layout.item_dialog_bottom_recycle_view);
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(mContent);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(commonDialogRecycleAdapter);
        commonDialogRecycleAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {
                mItemClink.itemTextClink(i,baseQuickAdapter.getData().get(i).toString(), "id",mType+"");
                dismiss();
            }
        });
        commonDialogRecycleAdapter.setNewData(listText);
    }





    @Override
    public void showAsDropDown(View anchor) {
        if(Build.VERSION.SDK_INT >= 24) {
            Rect rect = new Rect();
            anchor.getGlobalVisibleRect(rect);
            int h = anchor.getResources().getDisplayMetrics().heightPixels - rect.bottom;
            setHeight(h);
        }
        super.showAsDropDown(anchor);
    }

    @Override
    public void showAsDropDown(View anchor, int xoff, int yoff) {
        if(Build.VERSION.SDK_INT >= 24) {
            Rect rect = new Rect();
            anchor.getGlobalVisibleRect(rect);
            int h = anchor.getResources().getDisplayMetrics().heightPixels - rect.bottom;
            setHeight(h);
        }
        super.showAsDropDown(anchor, xoff, yoff);
    }
}